package fr.enseirb.mycub.component.map.tramway;

import java.util.ArrayList;
import java.util.Hashtable;
import android.content.Context;
import android.content.res.Resources;
import android.database.Cursor;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.location.Location;
import android.util.Log;
import android.widget.FrameLayout;

import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;

import fr.enseirb.mycub.R;
import fr.enseirb.mycub.component.map.OverlayBundle;
import fr.enseirb.mycub.component.map.OverlayBundleItem;
import fr.enseirb.mycub.structures.Line;
import fr.enseirb.mycub.structures.SpecificLine;
import fr.enseirb.mycub.structures.tramway.TramwayStation;

public class TramStationsOverlay extends OverlayBundle {

	private ArrayList<? extends OverlayBundleItem> items = new ArrayList<TramStationOverlay>();
	static ClassicDrawable standard = null;
	static PinDrawable pin = null;
	static Paint mTextPaint = new Paint();
	static Paint mPinPaint = new Paint();
	static boolean mDrawText;

	int IMAGE_HEIGHT;

	public TramStationsOverlay(Resources resources, BitmapDrawable tramPin, MapView mv, Context context) {
		//this(resources,drawable1,mv,context);
		super(tramPin, mv, context);

		mTextPaint.setAntiAlias(true);
		mTextPaint.setTextSize(19);
		mTextPaint.setTextAlign(Align.RIGHT);
		mTextPaint.setColor(Color.BLACK);
		mTextPaint.setTypeface(Typeface.DEFAULT_BOLD);

		mPinPaint.setStyle(Paint.Style.FILL);
		//mPinPaint.setColor(Color.BLUE);
		mPinPaint.setAlpha(150);
		mPinPaint.setAntiAlias(true);
		
		Hashtable<String, BitmapDrawable> table = new Hashtable<String, BitmapDrawable>();
		BitmapDrawable ligneA = (BitmapDrawable) context.getResources().getDrawable(
				R.drawable.ia);
		BitmapDrawable ligneB = (BitmapDrawable) context.getResources().getDrawable(
				R.drawable.ib);
		BitmapDrawable ligneC = (BitmapDrawable) context.getResources().getDrawable(
				R.drawable.ic);
		table.put("A", ligneA);
		table.put("B", ligneB);
		table.put("C", ligneC);

		TramStationsOverlay.standard = new ClassicDrawable(resources, tramPin.getBitmap());
		TramStationsOverlay.standard.table = table;
		
		boundCenterBottom(standard);
		pin = new PinDrawable();
		ClassicDrawable.mPaint.setColor(Color.WHITE);
		IMAGE_HEIGHT = standard.getIntrinsicHeight();
		
		populate();
	}



	@Override
	public void setItems(ArrayList<? extends OverlayBundleItem> list) {
		items = list;
		setLastFocusedIndex(-1);
		populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		return (items.get(i));
	}

	public ArrayList<? extends OverlayBundleItem> getOverlayList() {
		return items;
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		if (!shadow) {
			if (mapView.getZoomLevel() >= 16) {
				mDrawText = true;
				setBalloonBottomOffset(IMAGE_HEIGHT);
			} else {
				mDrawText = false;
				setBalloonBottomOffset(0);
			}
			super.draw(canvas, mapView, shadow);
		}
	}

	@Override
	public int size() {
		return (items.size());
	}

	public ArrayList<? extends OverlayBundleItem> getOverlaysFromCursor(
			ArrayList<Cursor> stationsCursors, Location location, int distanceFilter) {
		
		Cursor stationsCursor = stationsCursors.get(0);
		Cursor lineCursor = stationsCursors.get(1);
		Cursor specificLinesCursor = stationsCursors.get(2);
		Cursor specificLineStationCursor = stationsCursors.get(3);
		
		ArrayList<Line> lines = Line.getLinesFromCursor(lineCursor);
		ArrayList<SpecificLine> specificLines = SpecificLine.getSpecificLinesFromCursor(lines, specificLinesCursor);
		ArrayList<TramwayStation> stations = TramwayStation.getSimpleStationFromCursor(specificLines, stationsCursor, specificLineStationCursor);
		
		ArrayList<OverlayBundleItem> overlays = new ArrayList<OverlayBundleItem>(
				stationsCursor.getCount());
		TramStationOverlay overlay;
		Location stationLocation = new Location("");
		float distance = 0;
		for(int i = 0; i < stations.size(); i++) {
		 	final TramwayStation station = stations.get(i);
			overlay = new TramStationOverlay(
					station.getGeoPoint(),
					station.getLine(),
					station.getId());


			if (distanceFilter != 0 && location != null) {
				stationLocation.setLatitude(((double) station.getGeoPoint().getLatitudeE6()) * 1E-6);
				stationLocation.setLongitude(((double) station.getGeoPoint().getLongitudeE6()) * 1E-6);
				distance = location.distanceTo(stationLocation);
				if (distance > distanceFilter) {
//					Log.i("TramStations", "distance : "+distance);
					continue;
				}
			}
			
			overlays.add(overlay);
		}
//		} while (stationsCursor.moveToNext());
		Log.i("TramstationsOverlay", "--> overlays : "+overlays.size());
		return overlays;
	}

	@Override
	protected boolean onBalloonTap(int index, OverlayItem item) {
		Log.i("TramStations", "ON BALLOON TAP");
		return true;
	}



	@Override
	protected FrameLayout createBalloonOverlayView() {
		BalloonOverlayViewStationsTram<OverlayItem> b =  new BalloonOverlayViewStationsTram<OverlayItem>(getMapView().getContext(),
				viewOffset);
		return b;
	}



}